home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / probe_printer.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-28  |  6.7 KB  |  291 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import cupshelpers
  5. from debug import *
  6. import socket
  7. import time
  8. import gtk
  9. from timedops import TimedOperation
  10. import subprocess
  11.  
  12. def wordsep(line):
  13.     words = []
  14.     escaped = False
  15.     quoted = False
  16.     in_word = False
  17.     word = ''
  18.     n = len(line)
  19.     for i in range(n):
  20.         ch = line[i]
  21.         if escaped:
  22.             word += ch
  23.             escaped = False
  24.             continue
  25.         
  26.         if ch == '\\':
  27.             in_word = True
  28.             escaped = True
  29.             continue
  30.         
  31.         if in_word:
  32.             if quoted:
  33.                 if ch == '"':
  34.                     quoted = False
  35.                 else:
  36.                     word += ch
  37.             elif ch.isspace():
  38.                 words.append(word)
  39.                 word = ''
  40.                 in_word = False
  41.             elif ch == '"':
  42.                 quoted = True
  43.             else:
  44.                 word += ch
  45.         quoted
  46.         if ch == '"':
  47.             in_word = True
  48.             quoted = True
  49.             continue
  50.         if not ch.isspace():
  51.             in_word = True
  52.             word += ch
  53.             continue
  54.     
  55.     if word != '':
  56.         words.append(word)
  57.     
  58.     return words
  59.  
  60.  
  61. class LpdServer:
  62.     
  63.     def __init__(self, hostname):
  64.         self.hostname = hostname
  65.         self.max_lpt_com = 8
  66.  
  67.     
  68.     def _open_socket(self):
  69.         port = 515
  70.         
  71.         try:
  72.             (host, port) = self.hostname.split(':', 1)
  73.         except ValueError:
  74.             host = self.hostname
  75.  
  76.         s = None
  77.         
  78.         try:
  79.             ai = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
  80.         except socket.gaierror:
  81.             ai = []
  82.  
  83.         for res in ai:
  84.             (af, socktype, proto, canonname, sa) = res
  85.             
  86.             try:
  87.                 s = socket.socket(af, socktype, proto)
  88.                 s.settimeout(0.5)
  89.             except socket.error:
  90.                 msg = None
  91.                 s = None
  92.                 continue
  93.  
  94.             
  95.             try:
  96.                 s.connect(sa)
  97.             except socket.error:
  98.                 msg = None
  99.                 s.close()
  100.                 s = None
  101.                 continue
  102.  
  103.         
  104.         return s
  105.  
  106.     
  107.     def probe_queue(self, name, result):
  108.         s = self._open_socket()
  109.         if not s:
  110.             return False
  111.         print name
  112.         
  113.         try:
  114.             s.send('\x02%s\n' % name)
  115.             data = s.recv(1024)
  116.             print repr(data)
  117.         except socket.error:
  118.             msg = None
  119.             print msg
  120.             
  121.             try:
  122.                 s.close()
  123.             except:
  124.                 pass
  125.  
  126.             return False
  127.  
  128.         if len(data) > 0 and ord(data[0]) == 0:
  129.             
  130.             try:
  131.                 s.send('\x01\n')
  132.                 s.close()
  133.             except:
  134.                 pass
  135.  
  136.             result.append(name)
  137.             return True
  138.         
  139.         try:
  140.             s.close()
  141.         except:
  142.             ord(data[0]) == 0
  143.  
  144.         return False
  145.  
  146.     
  147.     def get_possible_queue_names(self):
  148.         candidate = [
  149.             'PASSTHRU',
  150.             'ps',
  151.             'lp',
  152.             'PORT1',
  153.             '']
  154.         for nr in range(self.max_lpt_com):
  155.             candidate.extend([
  156.                 'LPT%d' % nr,
  157.                 'LPT%d_PASSTHRU' % nr,
  158.                 'COM%d' % nr,
  159.                 'COM%d_PASSTHRU' % nr])
  160.         
  161.         for nr in range(50):
  162.             candidate.append('pr%d' % nr)
  163.         
  164.         return candidate
  165.  
  166.     
  167.     def probe(self):
  168.         result = []
  169.         for name in self.get_possible_queue_names():
  170.             while gtk.events_pending():
  171.                 gtk.main_iteration()
  172.             found = self.probe_queue(name, result)
  173.             if not found and name.startswith('pr'):
  174.                 break
  175.             
  176.             time.sleep(0.1)
  177.         
  178.         return result
  179.  
  180.  
  181.  
  182. class PrinterFinder:
  183.     
  184.     def find(self, hostname, callback_fn):
  185.         self.hostname = hostname
  186.         self.callback_fn = callback_fn
  187.         op = TimedOperation(self._do_find)
  188.  
  189.     
  190.     def _do_find(self):
  191.         self._cached_attributes = dict()
  192.         for fn in [
  193.             self._probe_snmp,
  194.             self._probe_lpd,
  195.             self._probe_hplip]:
  196.             
  197.             try:
  198.                 fn()
  199.             continue
  200.             except Exception:
  201.                 e = None
  202.                 nonfatalException()
  203.                 continue
  204.             
  205.  
  206.         
  207.         self.callback_fn(None)
  208.  
  209.     
  210.     def _probe_snmp(self):
  211.         null = file('/dev/null', 'r+')
  212.         
  213.         try:
  214.             p = subprocess.Popen(args = [
  215.                 '/usr/lib/cups/backend/snmp',
  216.                 self.hostname], stdin = null, stdout = subprocess.PIPE, stderr = null)
  217.         except OSError:
  218.             e = None
  219.             if e == errno.ENOENT:
  220.                 return None
  221.             raise 
  222.         except:
  223.             e == errno.ENOENT
  224.  
  225.         (stdout, stderr) = p.communicate()
  226.         if p.returncode != 0:
  227.             return None
  228.         for line in stdout.split('\n'):
  229.             words = wordsep(line)
  230.             n = len(words)
  231.             if n == 5:
  232.                 (device_class, uri, make_and_model, info, device_id) = words
  233.             elif n == 4:
  234.                 (device_class, uri, make_and_model, info) = words
  235.             
  236.             device_dict = {
  237.                 'device-class': device_class,
  238.                 'device-make-and-model': make_and_model,
  239.                 'device-info': info }
  240.             if n == 5:
  241.                 device_dict['device-id'] = device_id
  242.             
  243.             device = cupshelpers.Device(uri, **device_dict)
  244.             self.callback_fn(device)
  245.             self._cached_attributes['device-make-and-model'] = make_and_model
  246.         
  247.  
  248.     
  249.     def _probe_lpd(self):
  250.         lpd = LpdServer(self.hostname)
  251.         for name in lpd.get_possible_queue_names():
  252.             found = lpd.probe_queue(name, [])
  253.             if found:
  254.                 uri = 'lpd://%s/%s' % (self.hostname, name)
  255.                 device_dict = {
  256.                     'device-class': 'network',
  257.                     'device-info': self.hostname }
  258.                 device_dict.update(self._cached_attributes)
  259.                 new_device = cupshelpers.Device(uri, **device_dict)
  260.                 self.callback_fn(new_device)
  261.             
  262.             if not found and name.startswith('pr'):
  263.                 break
  264.             
  265.             time.sleep(0.1)
  266.         
  267.  
  268.     
  269.     def _probe_hplip(self):
  270.         null = file('/dev/null', 'r+')
  271.         
  272.         try:
  273.             p = subprocess.Popen(args = [
  274.                 'hp-makeuri',
  275.                 '-c',
  276.                 self.hostname], stdin = null, stdout = subprocess.PIPE, stderr = null)
  277.         except OSError:
  278.             e = None
  279.             if e == errno.ENOENT:
  280.                 return None
  281.             raise 
  282.         except:
  283.             e == errno.ENOENT
  284.  
  285.         (stdout, stderr) = p.communicate()
  286.         if p.returncode != 0:
  287.             return None
  288.         uri = stdout.strip()
  289.  
  290.  
  291.